package com.gogowan.agroenterprise.contorl;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.support.v4.app.Fragment;

import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.utils.PalUtils;
import com.gogowan.agroenterprise.utils.Utils;

/**
 * @Description: 异步请求类
 * @author mdp
 * @date 2017年1月4日 下午14:10:35
 */
public class WeakRefAsyncTask extends MyAsyncTask {

	// private static final boolean DEBUG = PalUtils.DEBUG;
	private static final boolean DEBUG = false;

	private WeakReference<AsyncLoadListener> mRequestListenerRef;

	private AsyncLoadListener mRequestListener;

	public WeakRefAsyncTask(AsyncLoadListener requestListener) {
		if (requestListener instanceof Activity
				|| requestListener instanceof Fragment || true) {// true改为所有都弱 此时别传菊部变量
			this.mRequestListenerRef = new WeakReference<>(requestListener);
		} else {
			this.mRequestListener = requestListener;
		}
	}

	/**
	 * onDestroyed-this.cancel or AsyncLoadListener.onSuccess-isDestroyed warn
	 * 并不支持队列多次cancel 只用于onDestroy()
	 */
	public void cancel() {
		if (mRequestListenerRef != null) {
			mRequestListenerRef.clear();
		}
		if (mRequestListener != null) {
			mRequestListener = null;
		}
	}

	@Override
	protected Object doInBackground(Object... params) {
		if (DEBUG) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		InterActionModel interActionModel = (InterActionModel) params[0];
		String result;
		if("get".equals(interActionModel.getRequestType())){
			result = Utils.getCommit(interActionModel.getUrl());
		}else{
			result = Utils.postCommit(interActionModel.getUrl(),
					interActionModel.getMessage());

		}

		PalUtils.showLogE("----------------------------------postCommit-------------------------------------");
		interActionModel.setResult(result);
		return interActionModel;
	}

	@Override
	protected void onPostExecute(Object res) {
		PalUtils.showLogE("----------------------------------onPostExecute-------------------------------------");
		InterActionModel interActionModel = (InterActionModel) res;
		if (DEBUG) {
			PalUtils.showLogE("mRequestListenerRef == null"
					+ (mRequestListenerRef == null));
			if (mRequestListenerRef != null) {
				PalUtils.showLogE("mRequestListenerRef.get() == null"
						+ (mRequestListenerRef.get() == null));
			}
			PalUtils.showLogE("mRequestListener == null"
					+ (mRequestListener == null));
		}
		if (mRequestListenerRef != null && mRequestListenerRef.get() != null) {
			AsyncLoadListener asyncLoadListener = mRequestListenerRef.get();
			asyncLoadListener.onResult(asyncLoadListener,
					interActionModel.getResult(), interActionModel.getIndex());
		} else if (mRequestListener != null) {
			mRequestListener.onResult(mRequestListener,
					interActionModel.getResult(), interActionModel.getIndex());
		} else {
			if (DEBUG) {
				PalUtils.showLogE("----------------------------------------------------------------------");
				PalUtils.showLogE("mRequestListenerRef == null"
						+ (mRequestListenerRef == null));
				if (mRequestListenerRef != null) {
					PalUtils.showLogE("mRequestListenerRef.get() == null"
							+ (mRequestListenerRef.get() == null));
				}
				PalUtils.showLogE("mRequestListener == null"
						+ (mRequestListener == null));
			}
		}
		super.onPostExecute(res);
	}

	/**
	 * 请求回调
	 * */
	public interface AsyncLoadListener {
		void onResult(AsyncLoadListener asyncLoadListener, String response,
				int actionId);
	}

	/**
	 * Message等相关请求类
	 * */
	public static class InterActionModel {

		private int index;
		private String url;
		private Object message;
		private String requestType;
		private String result;

		public String getRequestType() {
			return requestType;
		}

		public void setRequestType(String requestType) {
			this.requestType = requestType;
		}

		/**
		 * 请求Id
		 * */
		public int getIndex() {
			return index;
		}

		/**
		 * 请求Id
		 * */
		public void setIndex(int index) {
			this.index = index;
		}

		/**
		 * PostUrl
		 * */
		public String getUrl() {
			return url;
		}

		/**
		 * PostUrl
		 * */
		public void setUrl(String url) {
			this.url = url;
		}

		/**
		 * Message
		 * */
		public Object getMessage() {
			return message;
		}

		/**
		 * Message
		 * */
		public void setMessage(Object message) {
			this.message = message;
		}

		/**
		 * 服务器响应
		 * */
		public String getResult() {
			return result;
		}

		/**
		 * 服务器响应
		 * */
		public void setResult(String result) {
			this.result = result;
		}
	}
}